using System;
using System.Collections.Generic;
using LitJson;
using MTUnity.Utils;
using Script.UI.Component;

namespace Script.UI
{
    public class Utils
    {
        public static List<HistogramDataBase> SetData<T>(JsonData jsonData, string fieldJson , int iBoundCount = 6) where  T : IHistogramData, new()
        {
            if (jsonData.IsArray)
            {
                List<HistogramDataBase> listData = new List<HistogramDataBase>();
                double dMax1 = 0;
                double dMax2 = 0;
                double min1 = 0;
                double min2 = 0;
                for (int i = 0; i < jsonData.Count; i++)
                {
                    T data = new T();
                    data.Current = JsonUtils.ToDouble(jsonData[i][fieldJson]);
                    data.Timestamp = JsonUtils.ToLong(jsonData[i][JsonFildName.Common.Timestamp]);
                    if (i < iBoundCount)
                    {
                        dMax1 = Math.Max(dMax1, data.Current);
                        min1 = Math.Min(min1, data.Current);
                    }
                    dMax2 = Math.Max(dMax2, data.Current);
                    min2 = Math.Min(min2, data.Current);
                    listData.Add(data as HistogramDataBase);
                }
                for (int i = 0; i < listData.Count; i++)
                {
                    (listData[i] as IHistogramData).Max = i < 6 ? dMax1 : dMax2;
                    (listData[i] as IHistogramData).Min = i < 6 ? min1 : min2;

                }

                return listData;
            }

            return null;
        }
        
        public static List<HistogramDataBase> SetData<T>(JsonData jsonData, string fieldJson, string fieldJson2) where  T : IHistogramData2, new()
        {
            if (jsonData.IsArray)
            {
                List<HistogramDataBase> listData = new List<HistogramDataBase>();
                double dMax1 = 0;
                double dMax2 = 0;
                double min1 = 0;
                double min2 = 0;
                for (int i = 0; i < jsonData.Count; i++)
                {
                    T data = new T();
                    data.Current = JsonUtils.ToDouble(jsonData[i][fieldJson]);
                    data.Current2 = JsonUtils.ToDouble(jsonData[i][fieldJson2]);
                    data.Timestamp = JsonUtils.ToLong(jsonData[i][JsonFildName.Common.Timestamp]);
                    if (i < 6)
                    {
                        dMax1 = Math.Max(dMax1, data.Current);
                        min1 = Math.Min(min1, data.Current);
                    }
                    dMax2 = Math.Max(dMax2, data.Current);
                    min2 = Math.Min(min2, data.Current);
                    listData.Add(data as HistogramDataBase);
                }
                for (int i = 0; i < listData.Count; i++)
                {
                    (listData[i] as IHistogramData).Max = i < 6 ? dMax1 : dMax2;
                    (listData[i] as IHistogramData).Min = i < 6 ? min1 : min2;

                }

                return listData;
            }

            return null;
        }
    }
}